背景
打印对账文件最后一行汇总信息,通过钉钉定时发送到运维群。顺便总结下 Linux 打印文本部分行内容的各种方法。
测试文本
# 生成测试文本内容
$ seq -f "%02g daodaotest" 1 10 > test.txt
# 查看测试文本内容,并显示行号
$ cat -n test.txt
1 01 daodaotest
2 02 daodaotest
3 03 daodaotest
4 04 daodaotest
5 05 daodaotest
6 06 daodaotest
7 07 daodaotest
8 08 daodaotest
9 09 daodaotest
10 10 daodaotest
$ awk '{print NR" "$0}' test.txt
1 01 daodaotest
2 02 daodaotest
3 03 daodaotest
4 04 daodaotest
5 05 daodaotest
6 06 daodaotest
7 07 daodaotest
8 08 daodaotest
9 09 daodaotest
10 10 daodaotest
打印前 N 行内容
# head 打印前 5 行内容
$ head -5 test.txt
$ head -n 5 test.txt
# sed 打印前 5 行内容
$ sed -n '1,5p' test.txt
# awk 打印前 5 行内容
$ awk 'NR4 && NR4 && NR |